package com.gomcarter.frameworks.mybatis.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifeng.repo.base.api.PageResult;

import java.util.Arrays;
import java.util.List;

/**
 * 分页对象转换
 * Created by daibing on 2022/8/2.
 */
@Deprecated
public class PageTransformer {

    public static <T> PageResult<T> transform(Page<T> page) {
        PageResult<T> result = new PageResult<>((int) page.getCurrent(), (int) page.getSize());
        result.setTotalPage((int) page.getTotal());
        result.setTotalSize((int) page.getTotal());
        result.setResult(page.getRecords());
        return result;
    }

    public static <T> PageResult<T> emptyPage(int pageNo, int pageSize) {
        return new PageResult<>(pageNo, pageSize);
    }

    @SafeVarargs
    public static <T> PageResult<T> pageByMemory(int pageNo, int pageSize, T... element) {
        PageResult<T> pageResult = new PageResult<>(pageNo, pageSize);
        pageResult.setTotalPage((element.length / pageSize + ((element.length % pageSize == 0) ? 0 : 1)));
        pageResult.setTotalSize(element.length);
        pageResult.setResult(Arrays.asList(element));
        return pageResult;
    }

    public static <T> PageResult<T> pageByMemory(int pageNo, int pageSize, List<T> elements) {
        PageResult<T> pageResult = new PageResult<>(pageNo, pageSize);
        pageResult.setTotalPage((elements.size() / pageSize + ((elements.size() % pageSize == 0) ? 0 : 1)));
        pageResult.setTotalSize(elements.size());
        pageResult.setResult(elements);
        return pageResult;
    }

    public static <T> PageResult<T> buildPage(int pageNo, int pageSize, long totalSize, List<T> elements) {
        PageResult<T> pageResult = new PageResult<>(pageNo, pageSize);
        pageResult.setTotalPage((int) (totalSize / pageSize));
        pageResult.setTotalSize(totalSize);
        pageResult.setResult(elements);
        return pageResult;
    }
}
